﻿using System;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.ManageWms
{
    public partial class StockAdjustForm : BaseForm
    {
        private StoGoodsStock _updateRecord;
        public StockAdjustForm(StoGoodsStock record)
        {
            InitializeComponent();

            SetValueToUI(record);
        }
        private void InitButtonImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void SetValueToUI(StoGoodsStock record)
        {
            this.tbStockCode.Text = record.Id;
            if (record.PackageQty >= 0)
            {
                this.nudPackageQty.Value = record.PackageQty;
            }
            else
            {
                this.nudPackageQty.Value = 0;
            }
            if (record.PieceQty >= 0)
            {
                this.nudPieceQty.Value = record.PieceQty;
            }
            else
            {
                this.nudPieceQty.Value = 0;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                StoGoodsStock record = new StoGoodsStock();
                record.Id = this.tbStockCode.Text.Trim();
                this._updateRecord = client.QueryGoodsStock(record);
                this._updateRecord.PackageQty = (int)this.nudPackageQty.Value;
                this._updateRecord.PieceQty = (int)this.nudPieceQty.Value;
                this._updateRecord.Remark = this.rtbRemark.Text.Trim();

                if (this._updateRecord.PackageQty == 0 && this._updateRecord.PieceQty == 0)
                {
                    if (MessageBox.Show("当前记录的整箱和零数均调整为0，该库存记录将被删除，是否继续?", "注意", MessageBoxButtons.OKCancel
                        ) == System.Windows.Forms.DialogResult.Cancel)
                    {
                        return;
                    }
                    if (client.DeleteGoodsStock(this._updateRecord))
                    {
                        MessageBox.Show("库存调整成功！");
                    }
                }
                else
                {
                    if (client.UpdateGoodsStock(this._updateRecord))
                    {
                        MessageBox.Show("库存调整成功！");
                    }
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
